package cn.app.controller.develop;

import cn.app.pojo.DevUser;
import cn.app.service.devuser.DevuserService;
import cn.app.tools.Constants;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.apache.log4j.Logger;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
@RequestMapping(value = "/dev")
public class DevUserController {
    @Resource
    private DevuserService devuserService;

    private Logger logger = Logger.getLogger(DevUserController.class);

    @RequestMapping(value = "/login")
    public  String login(){
        logger.debug("LoginController welcome AppInfoSystem develpor>>>>>>>>>>>>>>>>>>>>>>");
        return "devlogin";
    }

    @RequestMapping(value = "/dologin")
    public  String dologin(@RequestParam String devCode, @RequestParam String devPassword, HttpSession session, HttpServletRequest request) throws Exception {
        logger.debug("Dologin welcome AppInfoSystem develpor>>>>>>>>>>>>>>>>>>>>>>");
        DevUser devUser=null;
        devUser=devuserService.Login(devCode,devPassword);
        if(null!=devUser){
            session.setAttribute(Constants.DEV_USER_SESSION,devUser);
            return "redirect:/dev/flatform/flatform";
        }else {
            //页面跳转（login.jsp）带出提示信息--转发
            request.setAttribute("error", "用户名或密码不正确");
            return "devlogin";
        }
    }

    @RequestMapping(value = "/flatform/flatform")
    public  String main(HttpSession session){
        logger.debug("LoginController welcome AppInfoSystem develpor>>>>>>>>>>>>>>>>>>>>>>");
        if(session.getAttribute(Constants.DEV_USER_SESSION) == null) {
            return "redirect:/dev/login";
        }
        return "developer/main";
    }

    @RequestMapping(value="/logout")
    public String logout(HttpSession session){
        //清除session
        session.removeAttribute(Constants.DEV_USER_SESSION);
        return "devlogin";
    }

}
